<template>
  <l-layout class="l-tab-page" v-if="!lr_loadPage">
    <template #left>
      <l-panel style="padding-right: 0">
        <template #title>
          {{ $t("模板分类") }}
          <div class="tree-setting-btn">
            <el-tooltip effect="dark" :content="$t('设置')" placement="top">
              <el-button
                @click="handleSettingClick"
                type="text"
                icon="el-icon-setting"
              ></el-button>
            </el-tooltip>
          </div>
        </template>
        <div class="l-rblock" style="padding-bottom: 32px">
          <div class="l-rblock" style="overflow: hidden auto">
            <el-tree
              :data="treeData"
              :expand-on-click-node="false"
              @node-click="handleNodeClick"
            >
              <span class="lr-tree-node" slot-scope="{ node }">
                <i class="el-icon-notebook-2"></i>
                {{ node.label }}
              </span>
            </el-tree>
          </div>
          <div class="l-bottom">
            <el-input
              :placeholder="$t('请输入名称')"
              v-model="searchCategoryWord"
              size="mini"
            >
            </el-input>
          </div>
        </div>
      </l-panel>
    </template>
    <l-panel style="padding-left: 0">
      <template #toolLeft>
        <div class="l-panel--item">
          <el-input
            :placeholder="$t('请输入名称/编码')"
            v-model="keyWord"
            @keyup.enter.native="hanleSearch"
            size="mini"
          >
            <el-button
              @click.native="hanleSearch"
              slot="append"
              icon="el-icon-search"
            ></el-button>
          </el-input>
        </div>
      </template>
      <template #toolRight>
        <l-tool-btns @click="handleAdd()" btnText="新增"></l-tool-btns>
      </template>
      <l-table
        :loading="tableLoading"
        :columns="columns"
        :dataSource="tableData"
        row-key="F_Id"
        :isPage="true"
        :pageTotal="tableTotal"
        :tablePage.sync="tableCurrentPage"
        @loadPageData="turnTablePage"
      >
        <template v-slot:f_EnabledMark="scope">
          <el-switch
            :active-value="1"
            :inactive-value="0"
            v-model="scope.row.f_EnabledMark"
            @change="handleEnableChange(scope.row)"
          >
          </el-switch>
        </template>
        <l-table-btns
          :btns="tableBtns"
          @click="lr_handleTableBtnClick"
        ></l-table-btns>
      </l-table>
    </l-panel>
    <l-fullscreen-dialog
      :title="formTitle"
      @ok="handleSave"
      @closed="handleClosed"
      @opened="handleOpened"
      :visible.sync="formVisible"
      ref="formDialog"
    >
      <my-form ref="form" />
    </l-fullscreen-dialog>
    <!-- 分类管理 -->
    <l-drawer
      :title="$t('分类管理')"
      :visible.sync="categoryVisible"
      :showOk="false"
      :showClose="false"
      size="800px"
    >
      <l-dataitem-index classifyCode="printSort"></l-dataitem-index>
    </l-drawer>
  </l-layout>
</template>

<script>
import MyForm from "./form";
const api = window.$api.wordPrint.template;
export default {
  components: {
    MyForm,
  },
  data() {
    return {
      searchCategoryWord: "",
      categoryVisible: false,
      categoryId: "",

      keyWord: "",
      tableData: [],
      tableLoading: false,
      tableTotal: 0,
      tablePageSize: 50,
      tableCurrentPage: 1,
      // 表格
      columns: [
        { label: "编码", prop: "f_Code", width: "150" },
        { label: "名称", prop: "f_Name", minWidth: "150" },
        {
          label: "分类",
          prop: "f_Category",
          width: "120",
          dataType: "dataItem",
          dataCode: "printSort",
          align: "center",
        },
        { label: "状态", prop: "f_EnabledMark", width: "64", align: "center" },
        {
          label: "最后修改人",
          prop: "f_ModifyUserName",
          width: "100",
          align: "center",
        },
        { label: "最后修改时间", prop: "f_ModifyDateTime", width: "160",dataType:'datetime' },
      ],
      tableBtns: [
        { prop: "Edit", label: "编辑",width:40 },
        { prop: "Delete", label: "删除",width:40 }
      ],

      formEdit: false,
      formEditRow: null,
      formVisible: false,
      formTitle: "",
    };
  },
  mounted() {
    this.lr_loadDataItem("printSort", false);
    this.loadTablePageData();
  },
  computed: {
    printSortList() {
      return this.lr_dataItem["printSort"] || [];
    },
    treeData() {
      return this.lr_dataItemTree(
        this.printSortList.filter(
          (t) => t.f_ItemName.indexOf(this.searchCategoryWord) > -1
        )
      );
    },
  },
  methods: {
    hanleSearch() {
      this.loadTablePageData();
    },
    loadTablePageData(isNotFirst) {
      if (!isNotFirst) {
        this.tableCurrentPage = 1;
      }
      this.tableLoading = true;
      let queryData = {
        rows: this.tablePageSize,
        page: this.tableCurrentPage,
        sidx:'f_ModifyDateTime DESC',
        f_Type: this.categoryId,
        keyword: this.keyWord,
      };
      api
        .getPage(queryData)
        .then((res) => {
          const tableData = this.$deepClone(res.data.data);
          this.tableData = tableData.rows;
          this.tableTotal = tableData.records;
          this.tableLoading = false;
        })
        .catch(() => {
          this.tableData = [];
          this.tableLoading = false;
        });
    },
    turnTablePage({ rows }) {
      this.tablePageSize = rows;
      this.loadTablePageData(true);
    },

    handleAdd() {
      this.formEdit = false;
      this.showForm("新增模板");
    },
    handleEdit($index, row) {
      this.formEdit = true;
      this.formEditRow = row;
      this.showForm(`编辑模板【${this.formEditRow.f_Name}】`);
    },
    handleDelete($index, row) {
      this.$confirm(this.$t("此操作将永久删除该数据, 是否继续?"), this.$t("提示"), {
        confirmButtonText: this.$t("确定"),
        cancelButtonText: this.$t("取消"),
        type: "warning",
      }).then(() => {
        api
          .remove(row.f_Id)
          .then(() => {
            this.$message({
              type: "success",
              message: this.$t("删除成功!"),
            });
            this.loadTablePageData();
          })
          .catch(() => {});
      });
    },

    handleSettingClick() {
      this.categoryVisible = true;
    },
    handleNodeClick(data) {
      this.categoryId = data.value;
      this.loadTablePageData();
    },

    showForm(text) {
      this.formTitle = text;
      this.formVisible = true;
    },
    async handleSave(showLoading, hideLoading) {
      showLoading("保存数据中...");
      if (await this.$refs.form.validateForm()) {
        const res = await this.$refs.form.saveForm({
          keyValue: this.formEdit ? this.formEditRow.f_Id : "",
          isEdit: this.formEdit,
        });
        if (res) {
          const msg = this.formEdit ? "更新成功!" : "新增成功!";
          this.$message({
            type: "success",
            message: this.$t(msg),
          });
          this.loadTablePageData();
          this.formVisible = false;
        }
      }
      hideLoading();
    },
    handleClosed() {
      this.$refs.form.resetForm();
    },
    async handleOpened(showLoading, hideLoading) {
      if (this.formEdit) {
        showLoading("加载数据中...");
        await this.$refs.form.loadFormData({ keyValue: this.formEditRow.f_Id });
        hideLoading();
      }
    },

    handleEnableChange(row){
      api.update(row.f_Id,row).then(() => {
        this.$message({
          type: "success",
          message: this.$t("更新成功!"),
        });
      });
    }
  },
};
</script>
